package leetcode.code0600;

public class Solution extends Solution600 {

	private static int N = 32;

	private static int[] dp;

	static {
		dp = new int[N];
		dp[0] = 1;// ?
		dp[1] = 2;// 0,1
		dp[2] = 3;// 0,1,2
		for (int i = 3; i < N; i++) {
			dp[i] = dp[i - 1] + dp[i - 2];
		}
	}

	private int getLen(int n) {
		for (int i = 31; i >= 0; i--) {
			if (((n >> i) & 1) == 1)
				return i;
		}
		return 0;
	}

	@Override
	public int findIntegers(int n) {
		int ans = 0;
		int len = getLen(n);
		int pre = 0;
		for (int i = len; i >= 0; i--) {
			int cur = (n >> i) & 1;
			if (cur == 1) {
				ans += dp[i];
			}
			if (pre == cur && cur == 1)
				return ans;
			pre = cur;
		}
		return ans + 1;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		System.out.println(Integer.toBinaryString(5));
	}

}
